import obj_types
import gtk
import parser.parser
import display.disp
#import display.test
import colors

from connectivity.client import Client

gtk.gdk.threads_init()

class LogInDisp(gtk.EventBox):
	parent = None
	
	def __init__(self, parent):
		gtk.EventBox.__init__(self)
		self.parent = parent
		
		self.set_border_width(10)
		self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.black))
		
		box = gtk.VBox(False, 3)
		box.set_border_width(10)
		self.add(box)
		box.show()
		
		box_fields = gtk.HBox(False, 0)
		box.pack_start(box_fields, False, False, 0)
		box_fields.show()
		
		box_labels = gtk.VBox(True, 3)
		box_fields.pack_start(box_labels, False, False, 0)
		box_labels.show()
		
		l_name = gtk.Label("Name:")
		box_labels.pack_start(l_name, False, False, 0)
		l_name.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.yellow))
		l_name.set_alignment(0.0, 0.5)
		l_name.show()
		
		l_pass = gtk.Label("Password: ")
		box_labels.pack_start(l_pass, False, False, 0)
		l_pass.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.yellow))
		l_pass.set_alignment(0.0, 0.5)
		l_pass.show()

		l_addr = gtk.Label("Address:  ")
		box_labels.pack_start(l_addr, False, False, 0)
		l_addr.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.yellow))
		l_addr.set_alignment(0.0, 0.5)
		l_addr.show()

		box_entries = gtk.VBox(True, 3)
		box_fields.pack_start(box_entries, False, False, 0)
		box_entries.show()
		
		self.e_pres = gtk.Entry()
		box_entries.pack_start(self.e_pres, False, False, 0)
		self.e_pres.show()
		
		self.e_pass = gtk.Entry()
		box_entries.pack_start(self.e_pass, False, False, 0)
		self.e_pass.show()
	
		self.e_class = gtk.Entry()
		box_entries.pack_start(self.e_class, False, False, 0)
		self.e_class.show()
		
		box_buttons = gtk.HBox(False, 3)
		box.pack_start(box_buttons, False, False, 0)
		box_buttons.show()
		
		btn_ok = display.disp.ColoredButton("OK")
		box_buttons.pack_end(btn_ok, False, False, 0)
		btn_ok.connect("clicked", self.btn_ok_clicked)
		btn_ok.show()
		
		btn_cancel = display.disp.ColoredButton("Cancel")
		box_buttons.pack_end(btn_cancel, False, False, 0)
		btn_cancel.connect("clicked", lambda wid: gtk.main_quit())
		btn_cancel.show()
		
	def btn_ok_clicked(self, widget):
		# IMPORTANT -- socket methods
		self.parent.tcp_client = Client(True)
		self.parent.tcp_client.connect(self.parent,self.e_class.get_text(), self.e_pres.get_text(), self.e_pass.get_text())
		if self.parent.tcp_client.last_error is None:
			self.parent.tcp_client.start_loop()
		# IMPORTANT -- socket methods
					
class ClDisp(gtk.EventBox):
	parent = None
	
	def __init__(self, parent):
		gtk.EventBox.__init__(self)
		self.parent = parent
		
		self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.darkgreen))
		self.show()
		
		self.content = gtk.VBox(False, 30)
		self.add(self.content)
		self.content.set_border_width(0)
		self.content.show()
		
		self.align = gtk.Alignment(0.5, 0.5, 1.0, 1.0)
		self.align.show()
		self.content.pack_start(self.align, True, True, 0)
		
		self.pres = display.disp.Disp([colors.black, [obj_types.obj_text.Text("")]])
		self.pres.show()
		self.align.add(self.pres)
		
		box_buttons = gtk.HBox(False, 0)
		self.content.pack_start(box_buttons, False, False, 0)
		box_buttons.show()
		
		btn_close = display.disp.ColoredButton("Close")
		box_buttons.pack_end(btn_close, False, False, 0)
		btn_close.connect("clicked", self.btn_close_clicked)
		btn_close.show()
		
	def btn_close_clicked(self, widget):
		# if there is a running tcp client close it
		if self.parent.tcp_client is not None:
			self.parent.tcp_client.close_client()
			self.parent.tcp_client = None
		gtk.main_quit()
		
	def display_slide(self, page):
		if page == None:
			self.parent.display_close()
		self.pres.destroy()
		self.pres = display.disp.Disp(page)
		self.pres.show()
		self.align.add(self.pres)
		
class CloseDisp(gtk.EventBox):
	parent = None
	
	def __init__(self, parent, text="Presentation has finished"):
		gtk.EventBox.__init__(self)
		self.parent = parent

		self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.black))
		
		box = gtk.VBox(False, 10)
		box.set_border_width(10)
		box.show()
		self.add(box)

		l_message = gtk.Label(text)
		l_message.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.yellow))
		l_message.show()
		box.pack_start(l_message, False, False, 0)
		
		box_buttons = gtk.HBox(False, 3)
		box_buttons.show()
		box.pack_start(box_buttons, False, False, 0)
		
		btn_close = display.disp.ColoredButton("Close")
		box_buttons.pack_end(btn_close, True, False, 0)
		btn_close.connect("clicked", lambda wid: gtk.main_quit())
		btn_close.show()

class MainWindow(gtk.Window):
	align = None
	login = None
	error = None
	close = None
	presentation = None
	tcp_client = None
	
	def __init__(self):
		gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
		self.connect("destroy", lambda wid : gtk.main_quit())
		self.fullscreen()
		self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.darkgreen))
		self.show()
		
		self.align = gtk.Alignment(0.5, 0.5, 0.0, 0.0)
		self.align.set_padding(0, 0, 0, 0)
		self.align.show()
		self.add(self.align)
		
		self.login = LogInDisp(self)
		self.login.show()
		self.align.add(self.login)
		
		
	def display_login(self):
		if self.error != None:
			self.error.destroy()
		self.align.add(self.login)
		
		self.align.set(0.5, 0.5, 0.0, 0.0)
		self.align.set_padding(0, 0, 0, 0)
		
	def display_error(self, message):
		self.error = display.disp.ErrorDisp(self, message)
		self.error.show()
		self.align.remove(self.align.get_child())
		self.align.add(self.error)
		self.align.set(0.5, 0.5, 0.0, 0.0)
		self.align.set_padding(0, 0, 0, 0)
		
	def display_slide(self, page):
		# k2k start
		if self.presentation == None:
			self.presentation = ClDisp(self)
			self.presentation.show()
		self.presentation.display_slide(page)
		self.align.remove(self.align.get_child())
		self.align.add(self.presentation)

		self.align.set(0.5, 0.5, 1.0, 1.0)
		self.align.set_padding(30, 30, 30, 30)
		# k2k end

		
	def display_close(self,text):	
		if self.close == None:
			self.close = CloseDisp(self,text)
			self.close.show()

		self.align.remove(self.align.get_child())
		self.align.add(self.close)
		

		self.align.set(0.5, 0.5, 0.0, 0.0)
		self.align.set_padding(0, 0, 0, 0)
		
def main():
	gtk.main()
	return 0

if __name__ == "__main__":
	MainWindow()
	main()